package Neighborhood;

import java.util.*;

public class ThresholdUserNeighborhood implements Neighborhood {
	
	private double thresholdValue=1.0d;

	@Override
	public Map<Integer, Double> getFilteredImages(
			HashMap<Integer, Double> map) {
		
		if(map==null)
			return null;
		final Map myHashMap = map;
		Comparator comparator = new Comparator(){
			public int compare(Object o1, Object o2) {
			// these will be string key objects paras
			// value has to be Comparable
				Integer key1 = (Integer)o1;//key1
				Integer key2 = (Integer)o2;//key2
				Double value1 = (Double)(myHashMap.get(o1));//value1
				Double value2 = (Double)(myHashMap.get(o2));//value2

			//sort on key as well if the value is different
			if(value1.equals(value2)) {
				return key1.compareTo(key2);
			} else {
				return (value2.compareTo(value1));
			}
			}
			};
		
			Map imageMap=new TreeMap(comparator);
			Map imageResultsMap=new HashMap<Integer, Double>();
			
			imageMap.putAll(myHashMap);
			Set set = imageMap.entrySet();
			Iterator i = set.iterator();
			while(i.hasNext()) {
		
				Map.Entry<Integer,Double> me = (Map.Entry<Integer,Double>)i.next();
				if(me.getValue().doubleValue()>= thresholdValue)
				{
					imageResultsMap.put(me.getKey(), me.getValue());
					
					System.out.println(me.getKey()+"  "+me.getValue());
				}
				
			} 
		return imageResultsMap;
	}

	public void setThresholdValue(double thresholdValue) {
		this.thresholdValue = thresholdValue;
	}

	public double getThresholdValue() {
		return thresholdValue;
	}

}
